package com.hangwei.paperless.adapter.accept;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.DictBean;
import com.hangwei.paperless.view.repair.AcceptCheck;
import com.twelve.all.utils.Tools;

/**
 * Created by 张龙臣 on 2020/11/18.
 */

public abstract class AcceptStatusAdapter extends RecyclerView.Adapter {
    private Context mContext;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext = viewGroup.getContext();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.pop_item_home_choose, viewGroup, false);
        return new RecyclerView.ViewHolder(inflate) {
        };
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ImageView ivChecked = viewHolder.itemView.findViewById(R.id.ivChecked);
        TextView tvTypeName = viewHolder.itemView.findViewById(R.id.tvTypeName);
        DictBean dictBean = AcceptCheck.mAcceptStatus.get(i);
        boolean hasChecked = false;
        for (DictBean bean : AcceptCheck.mAcceptStatus) {
            if (bean.checked)
                hasChecked = true;
        }
        if (!hasChecked)
            AcceptCheck.mAcceptStatus.get(0).checked = true;
        ivChecked.setVisibility(dictBean.checked ? View.VISIBLE : View.INVISIBLE);
        tvTypeName.setTextColor(dictBean.checked ? mContext.getResources().getColor(R.color.blue1) : mContext.getResources().getColor(R.color.text));
        tvTypeName.setText(Tools.checkedString(dictBean.name));
        final int position = i;
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!AcceptCheck.mAcceptStatus.get(position).checked) {
                    for (DictBean sh : AcceptCheck.mAcceptStatus) {
                        sh.checked = false;
                    }
                    AcceptCheck.mAcceptStatus.get(position).checked = true;
                }
                onAdapterItemClick(position);
                notifyDataSetChanged();
            }
        });
    }

    protected abstract void onAdapterItemClick(int position);

    @Override
    public int getItemCount() {
        return AcceptCheck.mAcceptStatus == null ? 0 : AcceptCheck.mAcceptStatus.size();
    }
}
